import request from './request'
import type {
  BackupCreateRequest,
  BackupRestoreRequest,
  CleanupExecuteRequest,
  AutoBackupConfigRequest,
  MaintenanceHistoryParams,
  LogQueryParams,
  LogAnalysisRequest,
  LogExportRequest,
  IntegrationActionRequest,
  ConnectionTestRequest,
  SyncDataRequest,
  AccessRuleRequest,
  ApiReportRequest,
  ApiResponse,
  PaginatedResponse
} from '@/types/system'

export interface SystemConfig {
  id?: number
  config_key: string
  config_value: string
  config_category: string
  config_type: string
  display_name: string
  description?: string
  default_value?: string
  validation_rule?: string
  group_name?: string
  sort_order?: number
  is_required?: boolean
  is_readonly?: boolean
  is_sensitive?: boolean
  is_active?: boolean
}

export interface ConfigHistory {
  id: number
  config_key: string
  old_value: string
  new_value: string
  change_type: string
  change_reason?: string
  operated_by: string
  operated_at: string
  client_ip?: string
}

export interface ConfigGroup {
  group_name: string
  category: string
  config_count: number
}

// 系统配置API
export const systemConfigApi = {
  // 获取配置列表
  getConfigs(params?: {
    page?: number
    size?: number
    category?: string
    group?: string
    keyword?: string
  }) {
    return request({
      url: '/api/v1/system/configs',
      method: 'get',
      params
    })
  },

  // 获取单个配置
  getConfig(key: string) {
    return request({
      url: `/api/v1/system/configs/${key}`,
      method: 'get'
    })
  },

  // 创建配置
  createConfig(data: SystemConfig) {
    return request({
      url: '/api/v1/system/configs',
      method: 'post',
      data
    })
  },

  // 更新配置
  updateConfig(key: string, data: Partial<SystemConfig>) {
    return request({
      url: `/api/v1/system/configs/${key}`,
      method: 'put',
      data
    })
  },

  // 删除配置
  deleteConfig(key: string) {
    return request({
      url: `/api/v1/system/configs/${key}`,
      method: 'delete'
    })
  },

  // 批量更新配置
  batchUpdateConfigs(data: Record<string, any>) {
    return request({
      url: '/api/v1/system/configs/batch',
      method: 'post',
      data
    })
  },

  // 获取配置历史
  getConfigHistory(key: string, limit = 50) {
    return request({
      url: `/api/v1/system/configs/${key}/history`,
      method: 'get',
      params: { limit }
    })
  },

  // 导出配置
  exportConfigs(params: {
    category?: string
    include_sensitive?: boolean
  }) {
    return request({
      url: '/api/v1/system/configs/export',
      method: 'post',
      data: params
    })
  },

  // 导入配置
  importConfigs(formData: FormData) {
    return request({
      url: '/api/v1/system/configs/import',
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 获取配置分组
  getConfigGroups(category?: string) {
    return request({
      url: '/api/v1/system/configs/groups',
      method: 'get',
      params: { category }
    })
  },

  // 测试配置连接
  testConnection(type: string, data: Record<string, any>) {
    return request({
      url: '/api/v1/system/configs/test-connection',
      method: 'post',
      data: { config_type: type, config_data: data }
    })
  },

  // 初始化默认配置
  initDefaultConfigs() {
    return request({
      url: '/api/v1/system/configs/init-defaults',
      method: 'post'
    })
  }
}

// 数据维护管理API
export const dataMaintenanceApi = {
  // 获取备份信息
  getBackupInfo() {
    return request({
      url: '/api/v1/system/data-maintenance/backup/info',
      method: 'get'
    })
  },

  // 创建备份
  createBackup(data: BackupCreateRequest) {
    return request({
      url: '/api/v1/system/data-maintenance/backup/create',
      method: 'post',
      data
    })
  },

  // 获取备份列表
  getBackupList() {
    return request({
      url: '/api/v1/system/data-maintenance/backup/list',
      method: 'get'
    })
  },

  // 恢复备份
  restoreBackup(data: BackupRestoreRequest) {
    return request({
      url: '/api/v1/system/data-maintenance/backup/restore',
      method: 'post',
      data
    })
  },

  // 下载备份
  downloadBackup(backupId: string) {
    return request({
      url: `/api/v1/system/data-maintenance/backup/download/${backupId}`,
      method: 'get',
      responseType: 'blob'
    })
  },

  // 删除备份
  deleteBackup(backupId: string) {
    return request({
      url: `/api/v1/system/data-maintenance/backup/delete/${backupId}`,
      method: 'delete'
    })
  },

  // 获取清理统计
  getCleanupStats() {
    return request({
      url: '/api/v1/system/data-maintenance/cleanup/stats',
      method: 'get'
    })
  },

  // 执行清理
  executeCleanup(data: CleanupExecuteRequest) {
    return request({
      url: '/api/v1/system/data-maintenance/cleanup/execute',
      method: 'post',
      data
    })
  },

  // 获取清理规则
  getCleanupRules() {
    return request({
      url: '/api/v1/system/data-maintenance/cleanup/rules',
      method: 'get'
    })
  },

  // 更新清理规则
  updateCleanupRules(data: Record<string, any>) {
    return request({
      url: '/api/v1/system/data-maintenance/cleanup/rules',
      method: 'put',
      data
    })
  },

  // 获取维护历史
  getMaintenanceHistory(params: MaintenanceHistoryParams) {
    return request({
      url: '/api/v1/system/data-maintenance/history',
      method: 'get',
      params
    })
  },

  // 更新自动备份配置
  updateAutoBackupConfig(data: AutoBackupConfigRequest) {
    return request({
      url: '/api/v1/system/data-maintenance/backup/auto-config',
      method: 'put',
      data
    })
  }
}

// 系统监控管理API
export const monitoringApi = {
  // 获取系统状态
  getSystemStatus() {
    return request({
      url: '/api/v1/system/monitoring/system/status',
      method: 'get'
    })
  },

  // 获取性能指标
  getPerformanceMetrics(timeRange = 24) {
    return request({
      url: '/api/v1/system/monitoring/system/performance',
      method: 'get',
      params: { time_range: timeRange }
    })
  },

  // 获取实时数据
  getRealtimeData() {
    return request({
      url: '/api/v1/system/monitoring/system/realtime',
      method: 'get'
    })
  },

  // 查询日志
  queryLogs(params: LogQueryParams) {
    return request({
      url: '/api/v1/system/monitoring/logs/query',
      method: 'get',
      params
    })
  },

  // 分析日志
  analyzeLogs(data: LogAnalysisRequest) {
    return request({
      url: '/api/v1/system/monitoring/logs/analyze',
      method: 'post',
      data
    })
  },

  // 导出日志
  exportLogs(data: LogExportRequest) {
    return request({
      url: '/api/v1/system/monitoring/logs/export',
      method: 'post',
      data
    })
  },

  // 获取日志统计
  getLogStatistics(timeRange = 24) {
    return request({
      url: '/api/v1/system/monitoring/logs/statistics',
      method: 'get',
      params: { time_range: timeRange }
    })
  }
}

// 系统集成管理API
export const integrationApi = {
  // 获取集成列表
  getIntegrationList() {
    return request({
      url: '/api/v1/system/integration/integrations',
      method: 'get'
    })
  },

  // 管理集成配置
  manageIntegration(data: IntegrationActionRequest) {
    return request({
      url: '/api/v1/system/integration/integrations/manage',
      method: 'post',
      data
    })
  },

  // 测试集成连接
  testConnection(data: ConnectionTestRequest) {
    return request({
      url: '/api/v1/system/integration/integrations/test',
      method: 'post',
      data
    })
  },

  // 数据同步
  syncData(data: SyncDataRequest) {
    return request({
      url: '/api/v1/system/integration/integrations/sync',
      method: 'post',
      data
    })
  },

  // 获取API监控数据
  getApiMonitoring(params: { time_range?: number; group_by?: string } = {}) {
    return request({
      url: '/api/v1/system/integration/api/monitoring',
      method: 'get',
      params
    })
  },

  // 获取API统计数据
  getApiStatistics(timeRange = 24) {
    return request({
      url: '/api/v1/system/integration/api/statistics',
      method: 'get',
      params: { time_range: timeRange }
    })
  },

  // 获取访问控制规则
  getAccessRules() {
    return request({
      url: '/api/v1/system/integration/api/access-rules',
      method: 'get'
    })
  },

  // 管理访问控制规则
  manageAccessRules(data: AccessRuleRequest) {
    return request({
      url: '/api/v1/system/integration/api/access-rules',
      method: 'post',
      data
    })
  },

  // 生成API报告
  generateReport(data: ApiReportRequest) {
    return request({
      url: '/api/v1/system/integration/api/reports',
      method: 'post',
      data
    })
  }
}